import math
from pandas import DataFrame

# hsv转rgb
def hsv2rgb(h, s, v):
    h = float(h)
    s = float(s)
    v = float(v)
    h60 = h / 60.0
    h60f = math.floor(h60)
    hi = int(h60f) % 6
    f = h60 - h60f
    p = v * (1 - s)
    q = v * (1 - f * s)
    t = v * (1 - (1 - f) * s)
    r, g, b = 0, 0, 0
    if hi == 0:
        r, g, b = v, t, p
    elif hi == 1:
        r, g, b = q, v, p
    elif hi == 2:
        r, g, b = p, v, t
    elif hi == 3:
        r, g, b = p, q, v
    elif hi == 4:
        r, g, b = t, p, v
    elif hi == 5:
        r, g, b = v, p, q
    r, g, b = int(r * 255), int(g * 255), int(b * 255)
    return r, g, b


# rgb转16#
def color(value):
    digit = list(map(str, range(10))) + list("ABCDEF")
    if isinstance(value, tuple):
        string = '#'
        for i in value:
            a1 = i // 16
            a2 = i % 16
            string += digit[a1] + digit[a2]
        return string


# 月份格式
def month_formatter(x, pos):
    v = int(x / 31) + 1
    if v > 12:
        return '-'
    else:
        return str(v)


# 计算事件发生连续天数
def continue_count(_df):
    
    _count = 0
    _index = 0
    _maxCount = 0
    _maxDayIndex = 0
    _maxDesc = ''
    for i in _df['dayIndex']:
        if i - _index == 1:
            _count += 1
            if _count > _maxCount:
                _maxCount = _count
                _maxDayIndex = i - _maxCount + 1
                _maxDesc = _df[_df['dayIndex'] == _maxDayIndex].index
                # print('maxDesc:', _maxDesc)
        else:
            _count = 1
        _index = i

    # print('====_df:', _df)
    # print('====_df:', _df['year'][7])
    # print('_maxCount:', _maxCount)
    # print('maxDayIndex:', _maxDayIndex)
    # print('maxDesc:', _maxDesc[0])

    _tdf = DataFrame({
        'year': [_df['year'][0]], 
        'maxCount': [_maxCount], 
        'maxDayIndex': [_maxDayIndex], 
        'maxDesc': _maxDesc[0]})
    return _tdf
